#!/usr/local/bin/perl
use FindBin '$Script';
use Getopt::Long;
use Cwd 'abs_path';
use Data::Dumper;
use strict;

my $PKGDB = '/var/db/pkg';

my $file = shift;
die "usage: $Script file\n" if !$file;
my $path = abs_path($file);

opendir(D, $PKGDB) || die "can't opendir $PKGDB: $!";
while(my $pkg = readdir(D))
{
  my $content = "$PKGDB/$pkg/+CONTENTS";
  open(F, $content) || next;
  my $cwd = '';
  while(<F>)
  {
    if (m{\@cwd (\S+)})
    {
      $cwd = $1;
      $path =~ s{^$cwd/}{};
      next;
    }
    next if !m{^$path$};
    print "$pkg\n";
    exit;
  }
  close(F);
}
closedir(D);
